// JavaScript Document
var discountWin1 = {
	scrollInfo: function() {
		var popBoxMacEl = document.getElementById("popBoxMac");
		if (popBoxMacEl) {
			popBoxMacEl.style.top = (document.documentElement.scrollTop || document.body.scrollTop) + 150 + "px";
		}
	},

	resizePopInfo: function() {
		var maskEl = document.getElementById("popMask");
		var bodyEl = document.getElementsByTagName("body")[0];
		var popBoxMacEl = document.getElementById("popBoxMac");
		if (maskEl && popBoxMacEl){	
			var width = popBoxMacEl.clientWidth;
			popBoxMacEl.style.marginLeft = "-" + (width / 2) + "px";

			maskEl.style.height = document.documentElement.clientHeight + "px";
			
			// 提示框top
			var infoTop = 0;
			var heightTmp = maskEl.clientHeight - popBoxMacEl.clientHeight;
			if(heightTmp > 0) {
				infoTop = heightTmp / 2;
			}
			discountWin1.scrollInfo();
		}
	},

	hideWin: function() {
		var bodyEl = document.getElementsByTagName("body")[0];
		var maskEl = document.getElementById("popMask");
		var popBoxMacEl = document.getElementById("popBoxMac");
		if (maskEl) {
			bodyEl.removeChild(maskEl);
		}
		if (popBoxMacEl) {
			bodyEl.removeChild(popBoxMacEl);
		}
	},

	showWin: function(target) {
		this.hideWin();
		
		var maskEl = document.createElement("div");
		var bodyEl = document.getElementsByTagName("body")[0];
		
		maskEl.className = "pop-mask";
		maskEl.id = "popMask";
		bodyEl.appendChild(maskEl);
		
		var infoEl = document.createElement("div");
		infoEl.className = "pop-box-mac";
		infoEl.id = "popBoxMac";
		infoEl.innerHTML =  '<div class="pop-bg"></div>' +
		    '<div class="pop-con layer-sm">' +
		        '<div class="pop-title">' +
		            '<b>上传附件</b>' +
		            '<a href="javascript:void(0);" onclick="return false;" class="close">×</a>' +
		        '</div>' +
		        '<div class="present">' +
					'<div class="upload-area c-upload-area">' +
			            '<input class="file-name" disabled="disabled">' + 
						'<div class="upload-wrap">' +
							'<input type="file" id="fileDom" size="0">' +
						'</div>'+
						'<div class="err-msg">13221</div>'+
						'<a class="select-file" href="javascript:void(0);" onclick="return false;">浏览</a>' +
						'<a class="upload-btn c-upload-btn" href="javascript:void(0);" onclick="return false;">上传</a>' +
						'<div class="c-upload-wrap">' +
							'<input class="c-file-name" type="text" id="" size="0">' +
						'</div>'+
						'<div class="c-tips-wrap">根据中央统一部署，2015年11月3日至12月31日，中央第十二巡视组对银监会党委进行了专项巡视。2016年2月2日，中央巡视组向银监会党委反馈了巡视意见。</div>'+
			        '</div>' +
				'</div>' +
		        '<div class="clear"></div>' +
		    '</div>';

		bodyEl.appendChild(infoEl);
		order_f.uploaddetail(target);
		this.resizePopInfo();
		this.initConfirmApply();
		this.initConfirmCancel();
	},

	// 线下还款通知
	showOfflineWin: function() {
		this.hideWin();
		
		var maskEl = document.createElement("div");
		var bodyEl = document.getElementsByTagName("body")[0];
		
		maskEl.className = "pop-mask";
		maskEl.id = "popMask";
		bodyEl.appendChild(maskEl);
		
		var infoEl = document.createElement("div");
		infoEl.className = "pop-box-mac";
		infoEl.id = "popBoxMac";
		infoEl.innerHTML = 
		    '<div class="pop-bg"></div>' +
		    '<div class="pop-con layer-sm">' +
		        '<div class="pop-title">' +
		            '<b>信息确认</b>' +
		            '<a href="javascript:void(0);" onclick="return false;" class="close">×</a>' +
		        '</div>' +
		        '<div class="present">' +
		            '<table>' +
		                '<tr>' +
		                    '<td>回单号：</td>' +
		                    '<td class="val">' +
		                        '<span class="num" id="receiptNo">123546789101112</span>' +
		                    '</td>' +
		                '</tr>' +
		                '<tr>' +
		                    '<td>汇款银行：</td>' +
		                    '<td class="val">' +
		                        '<span id="bank">上海银行</span>' +
		                    '</td>' +
		                '</tr>' +
		                '<tr>' +
		                    '<td>汇款帐号：</td>' +
		                    '<td class="val num" id="remittanceAccount">6227001100750070123</td>' +
		                '</tr>' +
		                '<tr>' +
		                    '<td>还款日期：</td>' +
		                    '<td class="val num" id="repaymentDay">2015.06.02</td>' +
		                '</tr>' +
		                '<tr>' +
		                    '<td>还款金额：</td>' +
		                    '<td class="val"><span class="money" id="repaymentAmount">500,000.00</span>元</td>' +
		                '</tr>' +
		            '</table>' +
		            '<p class="text-center mt10">' +
		            '<a class="apply-btn btn btn-primary" href="javascript:void(0);" onclick="return false;">确认提交</a>' +
		            '<a class="cancel-btn btn btn-default ml10" href="javascript:void(0);" onclick="return false;">取&nbsp;消</a>' +
		        	'</p>' +
		        '</div>' +
		        '<div class="clear"></div>' +
		    '</div>';

		bodyEl.appendChild(infoEl);
		discountWin1.setOfflineVal();
		discountWin1.resizePopInfo();
		discountWin1.initConfirmApply();
		discountWin1.initConfirmCancel();
	},

	setOfflineVal: function() {
		// TODO 设置弹出框内容
	},

	// 弹出框的确认提交
	initConfirmApply: function() {
		$(".present .apply-btn").click(function() {
			// TODO 确认提交
		});
	},

	// 弹出框的取消
	initConfirmCancel: function() {
		$(".present .cancel-btn, .pop-box-mac .close").click(function() {
			discountWin1.hideWin();
		});
	}
};

window.onresize = discountWin1.resizePopInfo;
window.onscroll = discountWin1.scrollInfo;
var order_f = (function () {
    return {
        //零钱贷管理-时间控件
        calendar: function(){
            if(!(typeof ECode === 'object' && typeof ECode.calendar === 'function')) return false;
				function formatNum(num){//补0
					return num.toString().replace(/^(\d)$/, "0$1");
				}
				function formatStrDate(vArg){//格式化日期
					switch(typeof vArg) {
						case "string":
							vArg = vArg.split(/-|\//g);
							return vArg[0] + "-" + formatNum(vArg[1]) + "-" + formatNum(vArg[2]);
							break;
						case "object":
							return vArg.getFullYear() + "-" + formatNum(vArg.getMonth() + 1) + "-" + formatNum(vArg.getDate());
							break;
					}
				};
				if($("#date01").val()=='')
				{
					}
        },
		//列表以及状态下拉框
		ulList: function(){
			$('.date-ul li').click(function(){
				$(this).addClass('on').siblings().removeClass('on');
			});
			$('.number-chose input').click(function(e){
				e.stopPropagation();
				$(this).blur().siblings('ul').show();
			});
			$('.number-chose li').hover(function(){
				$(this).addClass('hover');
			},function(){
				$(this).removeClass('hover');
			});
			$('.input-list input').click(function(e){
				e.stopPropagation();
				$(this).blur().siblings('ul').show();
			});
			$('.input-list li').hover(function(){
				$(this).addClass('hover');
			},function(){
				$(this).removeClass('hover');
			});
			$('.input-list li').click(function(){
				var val = $(this).text();
				var input = $(this).parents(".input-list").find("input");
				input.val(val);
				input.siblings(".err-msg").hide();
			});
			$('.number-chose li').click(function(){
				var val = $(this).text();
				$(this).parent().siblings('input').val(val);
			});
			$('.state-chose li').hover(function(){
				$(this).addClass('hover');
			},function(){
				$(this).removeClass('hover');
			});
			$('.state-chose').hover(function(){
				$(this).find('ul').show();
				$(this).find('p').addClass('on');
			},function(){
				$(this).find('ul').hide();
				$(this).find('p').removeClass('on');
			});
			$('.state-chose li').click(function(){
				var val = $(this).text();
				$(this).parent().siblings('p').text(val);
			});
			$(document).click(function(){
				$('.number-chose ul').hide();
				$('.state-chose ul').hide();
				$('.input-list ul').hide();
			});
			$('.type-radio').click(function(){
				$(this).addClass("selected").removeClass("unselected").siblings(".type-radio").addClass("unselected").removeClass("selected");
				$("#radio").attr("data-value", $(this).attr("data-value"));
				$(this).siblings('.err-msg').hide();
			});
		},

		companyMsgArea: null,

		//地址控件
		submitAddress: function(){
			if($('#area').length == 0)  return;
			//地址选择组件初始化
			order_f.companyMsgArea = $('#area').SnAddress({
				columns: [
					{state: 'prov', text: '请选择省', hide: false, addclass: ''},
					{state: 'city', text: '市', hide: false, addclass: ''}
					
				],
				url: 'http://www.suning.com/emall/SNAddressQueryCmd',
				exceptAreas:[],
				// exceptAreas:[{pro:'新疆'},{pro:'内蒙古'},{pro:'海南'},{pro:'青海'},{pro:'甘肃'},{pro:'西藏'},{pro:'宁夏'}],
				flag:false,
				complete: function() {
					$(".cityboxbtn").removeClass("err").parents(".row-content").find(".err-msg").hide();
				}
			}, [
				{name: '', code: '', id: ''},
				{name: '', code: '', id: ''}
				
			]).data('suning.address');


		},
		submitfuns:function(){
			$('.submit-change').click(function(){
				var errflag =false;
				$('input').trigger('blur');
				if($('#radio').attr('data-value') == 0){
					$('#radio').find('.err-msg').text('请选择公司类型').show();
				}
				$('.err-msg').each(function(index, element) {
					if(!$(this).hasClass('hide')) {
						errflag =true;
						return;
					}
				});
				$('.input-list input').each(function(index, element) {
					if($(this).val() == "") {
						$(this).siblings('.err-msg').show();
					}
				});

				if (order_f.companyMsgArea) {
					var address = order_f.companyMsgArea.getAddress();
					if (address[0].code == "") {
						$(".cityboxbtn").addClass("err").parents(".row-content").find(".err-msg").show();
						errflag =true;
					}
				}

				if(!errflag){
					discountWin1.showOfflineWin();
				}
			});
			$('.save-change').click(function(){
				
			});
			$('.submit-msg').click(function(){
				var errflag =false;
				$('input').trigger('blur');
				$('.input-list input').each(function(index, element) {
					var val = $(this).val();
					if($(this).val() == "") {
						$(this).siblings('.err-msg').show();
					}
				});
				
				if($('#radio').attr('data-value') == 0){
					$('#radio').find('.err-msg').text('请选择公司类型').show();
				}

				$('.err-msg').each(function(index, element) {
					if(!$(this).hasClass('hide')) {
						errflag =true;return;
					}
				});
				$('.err-msg').each(function(index, element) {
					if(!$(this).hasClass('hide')) {
						errflag =true;return;
					}
				});

				

				if(!errflag){

				}
			});
		},
		upload:function(){
			$('.mbtn-upld').click(function(){
				discountWin1.showWin($(this));
				});
			$('.delet-file').click(function(){
				var parent=$(this).parent().hide();
				parent.siblings('.mbtn-upld').show();
				});
			$('.upld-tips').hover(function(){
				$(this).find('div').show();
				},function(){
					
					$(this).find('div').hide();
					
					});
				
		},
		uploaddetail:function(target){
			var self=target;
			$("#popBoxMac .upload-btn").click(function() {
			
			// 如果要移除报错信息用下面的表达式
			// $("#popBoxMac .err-msg").hide();
			
			var fileName = $("#popBoxMac .file-name").val();
			// 如果没有选择文件，不允许上传文件
			if (fileName) {
				var dotIndex = fileName.lastIndexOf(".");
				// 后缀名
				var ext = fileName.substring(dotIndex + 1);
				// 判断是否为excel文件
				if (ext === "jpg" || ext === "rar") {
					self.hide();
					self.siblings('.upld-tips').hide();
					self.siblings('.uping').show();
					
					$.ajaxFileUpload({
						url: '',//用于文件上传的服务器端请求地址
						secureuri:false,//是否需要安全协议，一般设置为false
						fileElementId: 'fileDom',//文件选择框的id属性
						dataType:'json',//服务器返回的格式，可以是json
						success: function (data,status){//服务器成功响应处理函数
							// 如果不能成功上传
							if (true) {
								self.siblings('.upload-tip').find('.up-name').html(data.name);
								self.siblings('.uping').hide().siblings('.upload-tip').show();
							}
						},
						error: function(data){

						}
					});
					// 上传成功后提示信息
					$("#popBoxMac .err-msg").hide();
					discountWin1.hideWin();
					
				} else {
					// 在报错信息用以下方式显示
					$("#popBoxMac .err-msg").show().html("上传内容与要求不符，请重新上传");
				}
			} else {
				// 在报错信息用以下方式显示
				$("#popBoxMac .err-msg").show().html("请选择文件");
			}
			});
					/* ie8 不支持
					$("#fileDom").live('change', function() {
						$(".file-name").val($(this).val());
					});*/
					// 为了支持ie8
			document.getElementById("fileDom").onchange = function() {
					$(".file-name").val(this.value);
			}
		},
		changetab:function(){
			$('.ord-list .nav li').click(function(){
				$(this).addClass('on').siblings().removeClass('on');
				var index = $(this).index();
				$('.ord-detail').addClass('hide');
				$('.ord-detail').eq(index).removeClass('hide');
			})
				
		},

		initInput: function() {
			$(".input-f").focus(function() {
				$(this).addClass("focus");
			}).blur(function(){
				$(this).removeClass("focus");
			});
		},
		
        init: function () {
            this.calendar();
			this.upload();
			this.ulList();
			this.submitfuns();
			this.changetab();
			this.submitAddress();
			this.initInput();
        }
    }
})();

$(function() {
    order_f.init();
});
/* 放大镜 */
//全局的放大器
$(function(){
	bigInput.init({input:$('.bigInput')});
});
var bigInput = (function(){
	return {
		init : function(options){
			var options = this.options = options || {};
			this.input = options.input;
			this.numTip();
		},
		numTip : function(){ //数字放大效果显示&&类型判断
			var input = this.input,
				_body = input.parents().find('body'),
				_s1,_s2,_s3,_s4,_s5,_s6,_s7,_s8,_tipText;
			_body.append('<div class="mod-enlarge-num"></div>');
			var _dynNumTip = _body.find('.mod-enlarge-num');
			function subPone(_v){
				_s1 = _v.substring(0,3);
				_s2 = _v.substring(3,7);
				_s3 = _v.substring(7,11);
				_tipText = _s1+' '+_s2+' '+_s3;
				return _tipText;
			}
			function subCard(_v){
				_s1 = _v.substring(0,4);
				_s2 = _v.substring(4,8);
				_s3 = _v.substring(8,12);
				_s4 = _v.substring(12,16);
				_s5 = _v.substring(16,20);
				_tipText = _s1+' '+_s2+' '+_s3+' '+_s4 + ' '+_s5;
				return _tipText;
			}
			function subId(_v){
				_s1 = _v.substring(0,3);
				_s2 = _v.substring(3,6);
				_s3 = _v.substring(6,10);
				_s4 = _v.substring(10,14);
				_s5 = _v.substring(14,18);
				_tipText = _s1+' '+_s2+' '+_s3+' '+_s4 + ' '+_s5;
				return _tipText;
			}
			function showTip(_r,_v,_t){
				if(_r=='phone'){
					_t.attr('maxlength','11');
					_dynNumTip.text(subPone(_v));
				}else if(_r=='card'){
					_t.attr('maxlength','20');
					_dynNumTip.text(subCard(_v));
				}else if(_r=='id'){
					_t.attr('maxlength','18');
					_dynNumTip.text(subId(_v));
				}else{
					return false;	
				}
			}
			input.focus(function(){
				var _t = $(this),
					_v = _t.val().replace(/[ ]/g,''),
					_l = _v.length,
					_r = _t.attr('enlarge'),
					_x = _t.offset().left,
					_y = _t.offset().top,
					_fTxt = _v.substring(0,1),
					_reg =  /^[a-z0-9]+$/i;
				_dynNumTip.css({top:_y-37,left:_x});
				showTip(_r,_v,_t);
				if(_l>0){
					_dynNumTip.show();
				}
			}).keyup(function(){
				var _t = $(this),
				_v = _t.val().replace(/[ ]/g,''),
				_l = _v.length,
				_r = _t.attr('enlarge'),//[phone,card,id]:["手机","银行卡","身份证"];
				_x = _t.offset().left,
				_y = _t.offset().top;
				_dynNumTip.css({top:_y-37,left:_x});
				showTip(_r,_v,_t);
				if(_l==0){
					_dynNumTip.hide();
				}
				if(_l>0){
					_dynNumTip.show();	
				}
			}).blur(function(){
				_dynNumTip.hide();	
			});
		}
		
	}
})();